﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ZhiAo.Management.Data;

namespace ZhiAo.Management
{
    public class MenuInfo
    {
        /*
        *  dTree  add方法：
        *  第一个参数，表示当前节点的ID 
           第二个参数，表示当前节点的父节点的ID,根节点的值为 -1 
           第三个参数，节点要显示的文字 
           第四个参数，节点的Url 
           第五个参数，鼠标移至该节点时节点的Title 
           第六个参数，节点的target 
           第七个参数，用做节点的图标,节点没有指定图标时使用默认值 
           第八个参数，用做节点打开的图标,节点没有指定图标时使用默认值 
           第九个参数，判断节点是否打开 
        * */

        public static string GetMenu()
        {

            string xs = ""; int a = 0;
            string rootFunctionId = Utility.GetSpecificFunctionId();
            List<DataRow> functions = GetFunctionsByRootId(rootFunctionId);
            foreach (DataRow item in functions)
            {
                xs += "d.add('{0}','{1}','{2}','{3}','','content','Images/{4}.gif');";

                if (a != 0)
                {
                    xs = string.Format(xs, item["functionid"].ToString(), item["parentid"].ToString(),
                        item["name"].ToString(), item["url"].ToString() + "?functionId=" + item["functionid"].ToString(),
                        item["showinmenu"].ToString() == "1" ? "folderclose" : "page");
                }
                else
                {
                    xs = string.Format(xs, item["functionid"].ToString(), -1, item["name"].ToString(),
                        item["url"].ToString() + "?functionId=" + item["functionid"].ToString(),
                        item["showinmenu"].ToString() == "1" ? "folderclose" : "page");
                    a++;
                }

            }
            return xs;
        }

        /// <summary>
        /// 从当前用户的站点树中取特定站点的所有子站点(含该站点)
        /// </summary>
        /// <param name="functionId">特定站点id</param>
        /// <returns></returns>
        public static List<DataRow> GetFunctionsByRootId(string functionId)
        {
            List<DataRow> functionIds = new List<DataRow>();
            //DataTable functionTree = UserInfo.CurrentUserInfo.Functions;
            DataTable functionTree = UserQuery.GetFunctions(UserInfo.CurrentUserInfo.User.ID);
            string expression = "functionid='" + functionId + "'";
            DataRow[] currentRow = functionTree.Select(expression);
            functionIds.AddRange(currentRow);
            AddFunctionsToListByRootId(functionIds, functionTree, functionId);
            return functionIds;
        }


        public static List<DataRow> GetFunctionsByRootId(DataTable functionTree, string functionId)
        {
            List<DataRow> functionIds = new List<DataRow>();
            //DataTable functionTree = UserInfo.CurrentUserInfo.Functions;

            string expression = "functionid='" + functionId + "'";
            DataRow[] currentRow = functionTree.Select(expression);
            functionIds.AddRange(currentRow);
            AddFunctionsToListByRootId(functionIds, functionTree, functionId);
            return functionIds;
        }

        private static void AddFunctionsToListByRootId(List<DataRow> functionList, DataTable functionTree, string functionId)
        {
            string expression = "parentid='" + functionId + "'";
            DataRow[] rows = functionTree.Select(expression);
            foreach (DataRow dr in rows)
            {
                functionList.Add(dr);
                AddFunctionsToListByRootId(functionList, functionTree, dr["functionid"].ToString());
            }
        }

        public static List<DataRow> GetFunctionsOnlyLeaf(DataTable functionTree, string functionId)
        {
            List<DataRow> functionIds = new List<DataRow>();
            string expression = "functionid='" + functionId + "'";
            DataRow[] currentRow = functionTree.Select(expression);
            foreach (DataRow dr in currentRow)
            {
                if (dr["yr_stcd"] != DBNull.Value)
                {
                    functionIds.Add(dr);
                }
            }
           
            AddFunctionsToListOnlyLeaf(functionIds, functionTree, functionId);
            return functionIds;
        }

        private static void AddFunctionsToListOnlyLeaf(List<DataRow> functionList, DataTable functionTree, string functionId)
        {
            string expression = "parentid='" + functionId + "'";
            DataRow[] rows = functionTree.Select(expression);
            foreach (DataRow dr in rows)
            {
                if(dr["yr_stcd"]!=DBNull.Value)
                {
                    functionList.Add(dr);
                }

                AddFunctionsToListOnlyLeaf(functionList, functionTree, dr["functionid"].ToString());
            }
        }
    }
}
